import type cardType from "./cardType/cardType"
import { checkCardType } from "./cardType/cardType"
import { checkElement, type element } from "./element"


export default class roomData {
    roomId:string
    elementList: { [index: string]: element } = {}
    cardTypeList: cardType[] = []
    roomRoleList: string[] = []
    userList: string[] = []
    userRoleList: { [index: string]: string[] } = {}
    roomSetting: roomSetting = new roomSetting()
    constructor(){
        this.roomId = new Date().getTime()+''+Math.floor(Math.random()*100000000)
    }
}
export class roomSetting {
    imageServer: string = ''
}

export function checkRoomData(obj: any): roomData | undefined {
    if (typeof obj == 'object') {
        let result:roomData = new roomData()
        if (typeof obj.elementList == 'object') {
            for (const key of Object.keys(obj.elementList)) {
                let ele = checkElement(obj.elementList[key])
                if (ele != undefined) {
                    result.elementList[key] = ele
                }
            }
        }
        if (typeof obj.cardTypeList == 'object') {
            let cardTypeList: any = obj.cardTypeList
            if (cardTypeList instanceof Array) {
                result.cardTypeList = cardTypeList.flatMap(obj => checkCardType(obj)).filter(v => v != undefined)
            }
        }
        if (typeof obj.roomRoleList == 'object') {
            let roomRoleList: any = obj.roomRoleList
            if (roomRoleList instanceof Array) {
                result.roomRoleList = roomRoleList.filter(v => v != undefined).flatMap(obj => obj.toString())
            }
        }
        if (typeof obj.userList == 'object') {
            let userList: any = obj.userList
            if (userList instanceof Array) {
                result.userList = userList.filter(v => v != undefined).flatMap(obj => obj.toString())
            }
        }
        if (typeof obj.userRoleList == 'object') {
            for (const key of Object.keys(obj.userRoleList)) {    
                let roleList: any =  obj.userRoleList[key]
                if (typeof roleList == 'object') {
                    if (roleList instanceof Array) {
                        result.userRoleList[key] = roleList.filter(v => v != undefined).flatMap(obj => obj.toString())
                    }
                }
            }
        }
        if (typeof obj.roomSetting == 'object') {
            let rs: any = checkRoomSetting(obj.roomSetting)
            if (rs!= undefined) {
                result.roomSetting = rs
            }
        }
        if (typeof obj.roomId == 'string'){
            result.roomId = obj.roomId
        }
        return result
    } else {
        return
    }
}
export function checkRoomSetting(obj: any): roomSetting | undefined {
    if (typeof obj == 'object') {
        let result:roomSetting =new roomSetting()
        if (typeof obj.imageServer == 'string') {
            result.imageServer = obj.imageServer
        }
        return result
    } else {
        return
    }
}